<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
const 2
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

const X = 3

func main() {
	const (
		X = X + X
		Y
	)
	
	println(X, Y)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">6 6</label></li>
<li><label for="choiceB">6 12</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 6 12
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/xnB620_y0kc">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
Go spec says: Within a parenthesized const declaration list the expression list may be omitted from any but the first ConstSpec. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
a local identifier will shadow the global identifier with the same name.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
The consntant declaration in the quiz code is equivalent to the following one:
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">	const (
		X = X + X // here the two "X" are both the global one
		Y = X + X // here the two "X" are both the local one
	)
</code></pre>
<p></p>
<div class="tmd-usual">
The local <code class="tmd-code-span">X</code> is evaluated as 6 at compile time, so the constant <code class="tmd-code-span">Y</code> is evaluaed as 12 (also at compile time).
</div>
<p></p>
<div class="tmd-usual">
Please note that, <a href="https://github.com/golang/go/issues">the output result was <code class="tmd-code-span">6 6</code> when using Go toolchain v1.17-</a>. The bug has been fixed since Go toochain v1.18.
</div>
<p></p>
<div class="tmd-usual">
Similarly, since Go toolchain 1.18, the following program prints <code class="tmd-code-span">0 0</code>. Before Go toolchain 1.18, it printed <code class="tmd-code-span">1 2</code> (a bug).
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
	const (
		iota = iota
		X
		Y
	)
	println(X, Y)
}
</code></pre>
<p></p>
</div>
<p></p>
</div>
